package servlet;

import eneity.News;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import service.UserManage;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

public class addNewsServlet extends HttpServlet {
    private static final Object UPLOAD_DIRECTORY ="upload" ;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        News n=new News();
        req.setCharacterEncoding("utf-8");
        if (!ServletFileUpload.isMultipartContent(req)) {
            // 如果不是则停止
            PrintWriter writer = resp.getWriter();
            writer.println("Error: 表单必须包含 enctype=multipart/form-data");
            writer.flush();
            return;
        }
        // 配置上传参数
        ServletFileUpload upload = null;
        HashMap<String,String> newsInfo=new HashMap<String,String>();
        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            List<FileItem> formItems = upload.parseRequest(req);
            if (formItems != null && formItems.size() > 0) {

                // 迭代表单数据
                for (FileItem item : formItems) {
                    // 处理不在表单中的字段
                    if (!item.isFormField()) {
                        String fileName = new File(item.getName()).getName();
                        String uploadPath = req.getServletContext().getRealPath("./") + File.separator + UPLOAD_DIRECTORY;
                        String filePath = uploadPath + File.separator + fileName;
                        File storeFile = new File(filePath);
                        try {
                            item.write(storeFile);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        newsInfo.put("path",filePath);
                    }else {
                        newsInfo.put(item.getFieldName(),item.getString("utf-8"));
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        n.setNtitle(newsInfo.get("title"));
        n.setNauthor(newsInfo.get("author"));
        n.setNtid(newsInfo.get("selectID"));
        n.setNid(newsInfo.get("path"));
        n.setNssmmary(newsInfo.get("summany"));
        n.setNcontent(newsInfo.get("mainText"));
        UserManage userManage = new UserManage();
        int result = userManage.addNews(n);
        if (result>0){
            req.getRequestDispatcher("topicInfoServlet").forward(req,resp);
        }

    }
}
